<?php

namespace App\Console\Commands\PolicyUpload;

use App\Repositories\Contracts\PolicyInterface;
use App\Repositories\Contracts\PolicyUploadInterface;
use Illuminate\Console\Command;

class PolicyDeleteByPolicyCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'delByPolicyCode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(PolicyInterface $policyInterface,PolicyUploadInterface $policyUploadInterface)
    {
        //
        $policies = $policyInterface->get(['status'=>1]);
        if ($policies->isNotEmpty()){

            foreach ($policies as $key=>$value) {
                $saleTimeEnable = explode('-',$value['sale_time_enable']);
                if ($saleTimeEnable && is_array($saleTimeEnable) && count($saleTimeEnable) == 2) {

                    if (!(($saleTimeEnable[0] == "0000" && $saleTimeEnable[1] == "0000") || (date('Hi') >= $saleTimeEnable[0] && date('Hi') <= $saleTimeEnable[1]))) {

                        $policyUploadInterface->deletePolicy($value['platform'], $value['policy_code']);
                    }
                }

            }
        }
    }
}
